﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ConfigureSystem
{
    /// <summary>
    /// NxResizeGrip.xaml 的交互逻辑
    /// </summary>
    public partial class NxResizeGrip : ResizeGrip
    {
        private Window _BaseWindow;
        private FrameworkElement _TargetElement;
        private ResizeMode _ResizeMode;

        private double OffsetX;
        private double OffsetY;

        public ResizeMode ResizeMode
        {
            get { return _ResizeMode; }
            set
            {
                _ResizeMode = value;

                switch (_ResizeMode)
                {
                    case ResizeMode.Width:
                        this.Cursor = Cursors.SizeWE;
                        break;
                    case ResizeMode.Height:
                        this.Cursor = Cursors.SizeNS;
                        break;
                    case ResizeMode.WidthWithHeight:
                        this.Cursor = Cursors.SizeNWSE;
                        break;
                }
            }
        }

        public NxResizeGrip()
        {
            InitializeComponent();

            this.ResizeMode = ResizeMode.WidthWithHeight;
        }
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            _BaseWindow = Window.GetWindow(this);

            _TargetElement = (FrameworkElement)this.Parent;

            var MousePosition = e.GetPosition(_TargetElement);

            OffsetX = _TargetElement.ActualWidth - MousePosition.X;
            OffsetY = _TargetElement.ActualHeight - MousePosition.Y;

            

            _BaseWindow.MouseMove += BaseWindow_MouseMove;
            _BaseWindow.MouseUp += _BaseWindow_MouseUp;
            _BaseWindow.MouseLeave += _BaseWindow_MouseLeave;

            base.OnMouseDown(e);
        }

        private void _BaseWindow_MouseLeave(object sender, MouseEventArgs e)
        {
            _BaseWindow_MouseUp(sender, null);
        }

        private void _BaseWindow_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (_BaseWindow != null)
            {
                _BaseWindow.MouseMove -= BaseWindow_MouseMove;
                _BaseWindow.MouseUp -= _BaseWindow_MouseUp;
                _BaseWindow.MouseLeave -= _BaseWindow_MouseLeave;

                _BaseWindow = null;
                _TargetElement = null;
            }
        }

        private void BaseWindow_MouseMove(object sender, MouseEventArgs e)
        {
            var MousePosition = e.GetPosition(_TargetElement);

            if ((ResizeMode & ResizeMode.Width) != 0)
            {
                _TargetElement.Width = Math.Max(MousePosition.X + OffsetX, 0);
            }
            if ((ResizeMode & ResizeMode.Height) != 0)
            {
                _TargetElement.Height = Math.Max(MousePosition.Y + OffsetY, 0);
            }
        }
    }
    public enum ResizeMode
    {
        Width = 0x1,
        Height = 0x2,
        WidthWithHeight = 0x3
    }
}